package com.yuan.blog.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yuan.blog.entity.SystemUser;
import com.yuan.blog.service.ISystemUserService;
import com.yuan.blog.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author yuan_boss
 * @since 2022-12-22
 */
@RestController
@RequestMapping("/system")
public class SystemUserController {

    @Autowired
    private ISystemUserService systemUserService;

    @PostMapping("/list")
    public R list(){
        return R.ok(systemUserService.list());
    }

//系统用户列表，分页展示
    @PostMapping("/page")
    public R page(@RequestBody Map<String,Object> map){
        Page<SystemUser> page = new Page<>();
        page.setCurrent(Integer.parseInt(map.get("current").toString()));
        page.setSize(Integer.parseInt(map.get("size").toString()));
        Object username = map.get("username");
        QueryWrapper<SystemUser> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("create_time");
        wrapper.eq("status",0);
        wrapper.like("username",username.toString());
        systemUserService.page(page,wrapper);
        return R.ok(page);
    }

    @PostMapping("/add")
    public R add(@RequestBody SystemUser systemUser){
        systemUser.setStatus(0);
        systemUser.setCreateTime(new Date());
        boolean result = systemUserService.save(systemUser);
        return R.ok(result);
    }
    @GetMapping("/info/{id}")
    public R add(@PathVariable("id") Integer id){
        QueryWrapper<SystemUser> wrapper = new QueryWrapper<>();
        wrapper.eq("status",0);
        wrapper.eq("id",id);
        SystemUser user = systemUserService.getOne(wrapper);
        return R.ok(user);
    }

    @PutMapping("/update")
    public R update(@RequestBody SystemUser systemUser){
        boolean result = systemUserService.updateById(systemUser);
        return R.ok(result);
    }

    @DeleteMapping("/delete/{id}")
    public R delete(@PathVariable("id") String id){
        boolean result = systemUserService.removeById(id);
        return R.ok(result);
    }

    @DeleteMapping("/deleteBatch")
    public R deleteBatch(@RequestBody List<String> ids){
        boolean result = systemUserService.removeBatchByIds(ids);
        return R.ok(result);
    }

}
